package com.emm.yixun.marketing.service.impl;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.CustomerService;
import com.emm.yixun.basic.service.DataPermissionService;
import com.emm.yixun.basic.service.PayDepositService;
import com.emm.yixun.basic.service.PriceSchemeDetailService;
import com.emm.yixun.basic.service.PriceSchemeService;
import com.emm.yixun.basic.service.ProjectBuildingService;
import com.emm.yixun.basic.service.ProjectCarareaService;
import com.emm.yixun.basic.service.ProjectCarportService;
import com.emm.yixun.basic.service.ProjectHouseTypeService;
import com.emm.yixun.basic.service.ProjectImgService;
import com.emm.yixun.basic.service.ProjectPremisesService;
import com.emm.yixun.basic.service.ProjectRoomService;
import com.emm.yixun.basic.service.ProjectUnitService;
import com.emm.yixun.basic.service.SignedService;
import com.emm.yixun.basic.service.SubscribeService;
import com.emm.yixun.common.constant.SysConstants;
import com.emm.yixun.common.model.Customer;
import com.emm.yixun.common.model.PayDeposit;
import com.emm.yixun.common.model.PriceScheme;
import com.emm.yixun.common.model.PriceSchemeDetail;
import com.emm.yixun.common.model.ProjectBuilding;
import com.emm.yixun.common.model.ProjectCararea;
import com.emm.yixun.common.model.ProjectCarport;
import com.emm.yixun.common.model.ProjectHouseType;
import com.emm.yixun.common.model.ProjectImg;
import com.emm.yixun.common.model.ProjectPremises;
import com.emm.yixun.common.model.ProjectRoom;
import com.emm.yixun.common.model.ProjectUnit;
import com.emm.yixun.common.model.SignedInfo;
import com.emm.yixun.common.model.Subscribe;
import com.emm.yixun.common.model.SubscribeInfo;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.BusinessCustomerInfo;
import com.emm.yixun.common.model.dto.MarketingRoomView;
import com.emm.yixun.common.model.dto.PayDepositDto;
import com.emm.yixun.common.model.dto.ProjectBuildingDto;
import com.emm.yixun.common.model.dto.ProjectCarareaDto;
import com.emm.yixun.common.model.dto.ProjectCarportDto;
import com.emm.yixun.common.model.dto.ProjectImgDto;
import com.emm.yixun.common.model.dto.ProjectPremisesDto;
import com.emm.yixun.common.model.dto.ProjectRoomDto;
import com.emm.yixun.common.model.dto.ProjectUnitDto;
import com.emm.yixun.common.model.dto.RoomFooterVo;
import com.emm.yixun.common.model.dto.RoomInfo;
import com.emm.yixun.common.model.dto.RoomMarketingQueryView;
import com.emm.yixun.common.model.dto.RoomView;
import com.emm.yixun.common.model.dto.SignedDto;
import com.emm.yixun.common.model.dto.SubscribeDto;
import com.emm.yixun.common.model.enums.AuditStatusEnum;
import com.emm.yixun.common.model.enums.BusinessTypeEnum;
import com.emm.yixun.common.model.enums.CustomerSexEnum;
import com.emm.yixun.common.model.enums.LevelEnum;
import com.emm.yixun.common.model.enums.RoomColorEnum;
import com.emm.yixun.common.model.enums.RoomStatusEnum;
import com.emm.yixun.common.model.enums.ValuationTypeEnum;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.utils.BeanUtil;
import com.emm.yixun.marketing.service.RoomMarketingServiceFacade;
@Service("roomMarketingServiceFacade")
@WebService(name="com.emm.yixun.marketing.service.RoomMarketingServiceFacade")
public class RoomMarketingServiceFacadeImpl implements RoomMarketingServiceFacade{
	
	private  final Logger   logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ProjectImgService projectImgService;
	
	@Autowired
	private PriceSchemeService priceSchemeService;
	
	@Autowired
	private ProjectHouseTypeService projectHouseTypeService;
	
	@Autowired
	private ProjectRoomService projectRoomService;
	
	@Autowired
	private ProjectBuildingService projectBuildingService;
	
	@Autowired
	private PriceSchemeDetailService priceSchemeDetailService;
	
	@Autowired
	private ProjectUnitService projectUnitService;
	
	@Autowired
	private ProjectPremisesService premisesService;
	
	@Autowired
	private ProjectCarareaService projectCarareaService;
	
	@Autowired
	private ProjectCarportService projectCarportService;
	
	@Autowired
	private PayDepositService payDepositService;
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private SubscribeService subscribeService;
	
	@Autowired
	private DataPermissionService dataPermissionService;
	
	@Autowired
	private SignedService signedService;
	
	@Override
	public List<MarketingRoomView> findMarketingList(String buildingId,String unitId,String roomId,String resourcesCode, Long userId) {
		List<MarketingRoomView> array = new ArrayList<MarketingRoomView>();
		try {
			boolean flag = true;
			Long rmId = null;
			Long bdId = null;
			
			// 数据权限
			String dataPermissionSql = "";
			if (SysConstants.openDataPermission) {
				String dataPermission = dataPermissionService.dataPermissionCondition(
						userId, resourcesCode, null, null);
				String[] dataPermissionArr = dataPermission.split(":");
				dataPermissionSql = dataPermissionArr[1];
			}
			
			ProjectUnitDto queryDto = new ProjectUnitDto();
			queryDto.setDataPermission(dataPermissionSql);
			if(!StringUtils.isEmpty(buildingId)){
				queryDto.setBuildingId(treeId(buildingId));
				ProjectRoomDto queryRoomDto = new ProjectRoomDto();
				queryRoomDto.setBuildingId(treeId(buildingId));
				List<ProjectRoom> room = projectRoomService.selectListByDto(queryRoomDto);
				if(room!=null&&room.size()>0){
					if(room.get(0).getUnitId()==null){
						bdId = treeId(buildingId);
						flag=false;
					}
				}
			}else if(!StringUtils.isEmpty(unitId)){
				queryDto.setId(treeId(unitId));
			}else if(!StringUtils.isEmpty(roomId)){
				ProjectRoom room = projectRoomService.selectById(treeId(roomId));
				if(room!=null&&room.getUnitId()!=null){
					queryDto.setId(room.getUnitId());
				}else{
					rmId=treeId(roomId);
					flag=false;
				}
			}
			if(flag){
				array=unitRoomList(queryDto, roomId);
				//房间页脚统计
				
			}else{
				array.add(vailaList(bdId, rmId));
			}
		} catch (Exception e) {
			logger.error("findMarketingList error: ", e);
		}
		return array;
	}
	
	
	
	
	public RoomView getRoomView(ProjectRoom roomObj){
		RoomView rmv = new RoomView();
		
		rmv.setRoomStatus(roomObj.getRoomStatus());
		rmv.setRoomName(roomObj.getRoomName());
		ProjectHouseType type = projectHouseTypeService.selectById(roomObj.getHouseTypeId());
		if(type!=null){
			rmv.setRoomType(type.getHouseTypeName());
		}
		PriceSchemeDetail priceScheme = priceSchemeDetailService.selectEntityByRoomId(roomObj.getId());
		if(priceScheme!=null){
			rmv.setPrice(priceScheme.getUnitPrice());
			rmv.setTotalPrice(priceScheme.getTotalPrice());
		}
		rmv.setBuildingArea(roomObj.getBuildingArea());
		rmv.setColor(RoomColorEnum.getByNo(roomObj.getRoomStatus()).getName());
		rmv.setId(LevelEnum.LevelEnum_5.getCode()+"_"+roomObj.getId());
		return rmv;
	}
	public Long treeId(String preId){
		Long bid = new Long(preId.subSequence(preId.indexOf("_")+1, preId.length()).toString());
		return bid;
	}

	/**
	* 方法描述 : 别墅列表
	* 创建者：ThinkPad 
	* 项目名称： emm-estate
	* 类名： ProjectUnitServiceFacadeImpl.java
	* 版本： v1.0
	* 创建时间： 2016年7月2日 上午10:56:42
	* @param bdId
	* @param rmId
	* @return List<MarketingRoomView>
	 */
	public MarketingRoomView vailaList(Long bdId,Long rmId){
		ProjectRoomDto roomDto = new ProjectRoomDto();
		roomDto.setId(rmId);
		roomDto.setBuildingId(bdId);
		MarketingRoomView rv = new MarketingRoomView();
		List<RoomView> rvList = new ArrayList<RoomView>();
		List<ProjectRoom> roomList = projectRoomService.selectListByDto(roomDto);
		for (ProjectRoom roomObj:roomList) {
			RoomView rmv =getRoomView(roomObj);
			rvList.add(rmv);
		}
		rv.setRoomView(rvList);
		if(bdId!=null){
			ProjectBuilding building = projectBuildingService.selectById(bdId);
			rv.setUnitName(building.getBuildingName());
		}else if(rmId!=null){
			ProjectRoom room = projectRoomService.selectById(rmId);
			ProjectBuilding building = projectBuildingService.selectById(room.getBuildingId());
			rv.setUnitName(building.getBuildingName());
		}
		return rv;
	}
	
	/**
	* 方法描述 : 查询楼下面单元和房间, 不包括别墅这种情况
	* 创建者：ThinkPad 
	* 项目名称： emm-estate
	* 类名： ProjectUnitServiceFacadeImpl.java
	* 版本： v1.0
	* 创建时间： 2016年7月2日 上午10:38:40
	* @param queryDto
	* @param roomId
	* @return List<MarketingRoomView>
	 */
	public List<MarketingRoomView> unitRoomList(ProjectUnitDto queryDto,String roomId){
		List<MarketingRoomView> array = new ArrayList<MarketingRoomView>();
		queryDto.setDelFlag("0");
		List<ProjectUnit> unitList = projectUnitService.selectListByDto(queryDto);
		for (ProjectUnit obj:unitList) {
			MarketingRoomView rv = new MarketingRoomView();
			rv.setUnitName(obj.getUnitName());
			//查询单元下的房间
			ProjectRoomDto roomQuery = new ProjectRoomDto();
			if(!StringUtils.isEmpty(roomId)){
				roomQuery.setId(treeId(roomId));
			}else{
				roomQuery.setUnitId(obj.getId());
			}
			roomQuery.setDelFlag("0");
			List<ProjectRoom> roomList = projectRoomService.selectListByDto(roomQuery);
			List<RoomView> addObj = new ArrayList<RoomView>();
			for (ProjectRoom roomObj:roomList) {
				RoomView rmv = getRoomView(roomObj);
				addObj.add(rmv);
				rv.setRoomView(addObj);
			}
			array.add(rv);
		}
		return array;
	}
	
	
	@Override
	public ResponseList<RoomMarketingQueryView> treeNodeQuery(Long projectId,Long premisesId,Long buildingId,String resourcesCode, Long userId) {
		ResponseList<RoomMarketingQueryView> responseList = new ResponseList<RoomMarketingQueryView>();
		try {
			// 数据权限
			String dataPermissionSql = "";
			if (SysConstants.openDataPermission) {
				String dataPermission = dataPermissionService.dataPermissionCondition(
						userId, resourcesCode, null, null);
				String[] dataPermissionArr = dataPermission.split(":");
				if ("00".equals(dataPermissionArr[0])) {
					responseList.setSuccess(true);
					return responseList;
				}
				dataPermissionSql = dataPermissionArr[1];
			}
			List<RoomMarketingQueryView> addObj = new ArrayList<RoomMarketingQueryView>();
			if(projectId!=null){
				ProjectPremisesDto entity = new ProjectPremisesDto();
				entity.setProjectId(projectId);
				entity.setDataPermission(dataPermissionSql);
				List<ProjectPremises> premisesList = premisesService.selectListByDto(entity);
				for (ProjectPremises premisesObj:premisesList) {
					RoomMarketingQueryView view = new RoomMarketingQueryView();
					view.setId(LevelEnum.LevelEnum_2.getCode()+"_"+premisesObj.getId());
					view.setName(premisesObj.getGroupName());
					addObj.add(view);
				}
				
				//车区
				ProjectCarareaDto queryEntity = new ProjectCarareaDto();
				queryEntity.setProjectId(projectId);
				queryEntity.setDataPermission(dataPermissionSql);
				List<ProjectCararea> carareaList = projectCarareaService.selectListByDto(queryEntity);
				for (ProjectCararea carArea:carareaList) {
					RoomMarketingQueryView view = new RoomMarketingQueryView();
					view.setId(LevelEnum.LevelEnum_6.getCode()+"_"+carArea.getId());
					view.setName(carArea.getCarareaName());
					addObj.add(view);
				}
			}else if(premisesId!=null){
				ProjectBuildingDto building = new ProjectBuildingDto();
				building.setPremisesId(premisesId);
				building.setDataPermission(dataPermissionSql);
				List<ProjectBuilding> buildingList = projectBuildingService.selectListByDto(building);
				for (ProjectBuilding buildingObj:buildingList) {
					RoomMarketingQueryView view = new RoomMarketingQueryView();
					view.setId(LevelEnum.LevelEnum_3.getCode()+"_"+buildingObj.getId());
					view.setName(buildingObj.getBuildingName());
					addObj.add(view);
				}
			}else if(buildingId!=null){
				ProjectUnitDto  projectUnitDto=new ProjectUnitDto();
				projectUnitDto.setBuildingId(buildingId);
				projectUnitDto.setDataPermission(dataPermissionSql);
				List<ProjectUnit> projectUnitList =projectUnitService.selectListByDto(projectUnitDto);
	            for(ProjectUnit projectUnit : projectUnitList){
	            	RoomMarketingQueryView view = new RoomMarketingQueryView();
					view.setId(LevelEnum.LevelEnum_4.getCode()+"_"+projectUnit.getId());
					view.setName(projectUnit.getUnitName());
					addObj.add(view);
	            }
			}
			responseList.setSuccess(true);
			responseList.setEntity(addObj);
		} catch (Exception e) {
			responseList.setSuccess(false);
			logger.error("treeNodeQuery error: ", e);
		}
		return responseList;
	}
	
	@Override
	public List<MarketingRoomView> findCarInfo(Long areaId, Long carportId) {
		try {
			List<MarketingRoomView> initList = new ArrayList<MarketingRoomView>();
			MarketingRoomView initObj = new MarketingRoomView();
			List<RoomView> roomView = new ArrayList<RoomView>();
			ProjectCarportDto carPortDto = new  ProjectCarportDto();
			carPortDto.setId(carportId);
			carPortDto.setCarareaId(areaId);
			List<ProjectCarport> carList = projectCarportService.selectListByDto(carPortDto);
			for (ProjectCarport carObj:carList) {
				RoomView rv = new RoomView();
				rv.setRoomStatus(carObj.getCarportSalesStatus());
				rv.setRoomName(carObj.getCarportName());
				rv.setColor(RoomColorEnum.getByNo(carObj.getCarportSalesStatus()).getName());
				PriceSchemeDetail priceScheme = priceSchemeDetailService.selectEntityByCarPortId(carObj.getId());
				if(priceScheme!=null){
					rv.setPrice(priceScheme.getUnitPrice());
					rv.setTotalPrice(priceScheme.getTotalPrice());
				}
				rv.setId(LevelEnum.LevelEnum_7.getCode()+"_"+carObj.getId());
				roomView.add(rv);
			}
			initObj.setRoomView(roomView);
			if(areaId!=null){
				ProjectCararea area = projectCarareaService.selectById(areaId);
				initObj.setUnitName(area.getCarareaName());
			}else if(carportId!=null){
				ProjectCarport port = projectCarportService.selectById(carportId);
				ProjectCararea area = projectCarareaService.selectById(port.getCarareaId());
				initObj.setUnitName(area.getCarareaName());
			}
			 initList.add(initObj);
			 return initList;
		} catch (Exception e) {
			logger.error("findCarInfo error: ", e);
		}
		return null;
	}


	@Override
	public RoomInfo getRoomInfo(Long id) {
		RoomInfo info = new RoomInfo();
		try {
			ProjectRoom room = projectRoomService.selectById(id);
			 info = BeanUtil.convert2T(info, room);
			 info.setRoomStatus(RoomStatusEnum.getByNo(room.getRoomStatus()).getDes());
			 PriceSchemeDetail detail = priceSchemeDetailService.selectEntityByRoomId(id);
			 if(detail!=null&&detail.getId()!=null){
				 info.setScheme_price(detail.getUnitPrice());
				 info.setScheme_total_price(detail.getTotalPrice());
				 PriceScheme priceSche= priceSchemeService.selectById(detail.getPriceSchemeId());
				 if(priceSche!=null){
					 info.setValuationType(ValuationTypeEnum.getByNo(priceSche.getValuationType()).getName());
					 if(priceSche.getValuationType().equals(ValuationTypeEnum.ValuationTypeEnum_1.getNo())||priceSche.getValuationType().equals(ValuationTypeEnum.ValuationTypeEnum_3.getNo())){
						 	//建筑单价
						 info.setBuildingPriceN(detail.getUnitPrice());
					 }else if(priceSche.getValuationType().equals(ValuationTypeEnum.ValuationTypeEnum_2.getNo())){
						 //套内单价
						 info.setInnerPriceN(detail.getUnitPrice());
					 }
				 }
			 }
			 //户型图
			 ProjectHouseType houseType =  projectHouseTypeService.selectById(room.getHouseTypeId());	
			 if(houseType!=null){
				 ProjectImgDto imgDto = new ProjectImgDto();
				 imgDto.setProjectId(houseType.getId());
				 imgDto.setProjectImgCode("houseTypeCode");
				 List<ProjectImg> projectImg= projectImgService.selectListByDto(imgDto);
				 if(projectImg!=null&&projectImg.size()>0){
					 info.setHouseTypeImg(projectImg.get(0).getNormalUrl());
				 }
			 }
		
			 return info;
		} catch (Exception e) {
			logger.error("getRoomInfo error: ", e);
		}
		return null;
	}




	@Override
	public List<RoomFooterVo> getRoomNumber(ProjectRoomDto room) {
		List<RoomFooterVo> array = new ArrayList<RoomFooterVo>();
		try {
			// 数据权限
			String dataPermissionSql = "";
			if (SysConstants.openDataPermission) {
				String dataPermission = dataPermissionService.dataPermissionCondition(
						room.getPermissionUserId(), room.getResourcesCode(), null, null);
				String[] dataPermissionArr = dataPermission.split(":");
				dataPermissionSql = dataPermissionArr[1];
			}
			room.setDataPermission(dataPermissionSql);
			for(int i=0;i<RoomColorEnum.values().length;i++ ){
				RoomFooterVo view = new RoomFooterVo();
				room.setRoomStatus(RoomColorEnum.values()[i].getNo());
				List<ProjectRoom> queryList=projectRoomService.selectListByDto(room);
				view.setStatus(RoomStatusEnum.getByNo(RoomColorEnum.values()[i].getNo()).getDes());
				view.setColor(RoomColorEnum.values()[i].getName());
				if(queryList!=null&&queryList.size()>0){
					view.setNumber(queryList.size());
				}else{
					view.setNumber(0);
				}
				array.add(view);
			}
		} catch (Exception e) {
			logger.error("getRoomNumber error: ", e);
		}
		return array;
	}



	@Override
	public List<BusinessCustomerInfo> findBusinessCustomerInfo(User user,Long roomId,String businessCode) {
		try {
			List<BusinessCustomerInfo> arrayList = new ArrayList<BusinessCustomerInfo>();
				PayDepositDto entity = new PayDepositDto();
				entity.setRoomId(roomId);
				entity.setAgentId(user.getId());
				entity.setAuditStatus(AuditStatusEnum.SUCCESS.getNo());
				List<PayDeposit> payDeposits = payDepositService.selectListByDto(entity);
				for (PayDeposit sit:payDeposits) {
					BusinessCustomerInfo info = new BusinessCustomerInfo();
					info.setBusinessId(sit.getId().toString());
					Customer customer = customerService.selectById(sit.getCustomerId());
					info.setCustomerName(customer.getCustomerName());
					CustomerSexEnum cusSex = CustomerSexEnum.getByNo(customer.getCustomerSex());
					if(cusSex!=null){
						info.setSex(cusSex.getDes());
					}
					info.setIdCardId(customer.getPeDocumentNumber());
					info.setMobile(customer.getCustomerPhone());
					info.setBusinessStatus(BusinessTypeEnum.DEPOSIT.getNo());
					info.setBtnBusiness(businessCode);
					info.setStatus(BusinessTypeEnum.DEPOSIT.getDes());
					arrayList.add(info);
			}
				//查询认购信息
				SubscribeDto subscribeEntity = new SubscribeDto(); 
				subscribeEntity.setRoomId(roomId);
				subscribeEntity.setAgentId(user.getId());
				subscribeEntity.setBusinessStatus("1");
				List<Subscribe> subcribeList = subscribeService.findBusinessInfo(subscribeEntity);
				for (Subscribe subscribe : subcribeList) {
					if(subscribe.getPayDepositId()!=null){ //去除重复的数据
						for (BusinessCustomerInfo removeObj : arrayList) {
							if(new Long(removeObj.getBusinessId()).compareTo(subscribe.getPayDepositId())==0){
								removeObj.setBusinessStatus(BusinessTypeEnum.SUBSCRIBE.getNo());
								removeObj.setBusinessId(subscribe.getId().toString());
								removeObj.setStatus(BusinessTypeEnum.SUBSCRIBE.getDes());
								removeObj.setBtnBusiness(businessCode);
							}
						}
					}else{
						BusinessCustomerInfo info = new BusinessCustomerInfo();
						info.setBusinessId(subscribe.getId().toString());
						Customer customer = customerService.selectById(subscribe.getCustomerId());
						info.setCustomerName(customer.getCustomerName());
						CustomerSexEnum cusSex = CustomerSexEnum.getByNo(customer.getCustomerSex());
						if(cusSex!=null){
							info.setSex(cusSex.getDes());
						}
						info.setIdCardId(customer.getPeDocumentNumber());
						info.setMobile(customer.getCustomerPhone());
						info.setBusinessStatus(BusinessTypeEnum.SUBSCRIBE.getNo());
						info.setBtnBusiness(businessCode);
						info.setStatus(BusinessTypeEnum.SUBSCRIBE.getDes());
						arrayList.add(info);
					}
				}
				return arrayList;
		} catch (Exception e) {
			logger.error("findBusinessCustomerInfo error: ", e);
		}
		return null;
	}
	/**
	 * 获取认购套数、金额、占比、签约套数、金额、占比
	 */
	@Override
	public Map<String, String> findProportion(Long projectId, Long premisesId, Long buildingId,Long unitId,
			Long roomId,String resourcesCode,Long userId){
		Map<String, String> resMap = new HashMap<String, String>();
		//根据条件查询全部房间
		ProjectRoomDto entity = new ProjectRoomDto();
		entity.setProjectId(projectId);
		entity.setPremisesId(premisesId);
		entity.setBuildingId(buildingId);
		entity.setUnitId(unitId);
		entity.setId(roomId);
		List<ProjectRoom> queryList = projectRoomService.selectListByDto(entity);
		if(queryList == null){
			logger.error("findProportion() ==》 未查询到房间信息");
			return resMap;
		}
		//拼接查询语句
		String sql = "";
		for(ProjectRoom pr : queryList){
			sql += " t.room_id='" + pr.getId() + "' or";
		}
		if(!"".equals(sql)){
			sql = sql.substring(0, sql.length()-2);
		}
		//获取总房价
		BigDecimal roomTotalAmount = getTotalPrice(sql); 
		//查询认购总数与总金额
		SubscribeDto subscribeEntity = new SubscribeDto();
		subscribeEntity.setRemark(sql);
		SubscribeInfo subscribeInfo = subscribeService.totalNumberAndAmount(subscribeEntity);
		if(subscribeInfo != null){
			//数量
			resMap.put("subscribeTotal", String.valueOf(subscribeInfo.getTotal()));
			resMap.put("subscribeTotalRatio", getRatio(new BigDecimal(subscribeInfo.getTotal()), 
					new BigDecimal(queryList.size())));
			//金额
			BigDecimal subscribeAmount = new BigDecimal("0");
			if(subscribeInfo.getDepositReceivable() != null){
				subscribeAmount = subscribeInfo.getDepositReceivable();
			}
			resMap.put("subscribeAmount", numberFormat(subscribeAmount, new BigDecimal(10000), 6));
			resMap.put("subscribeAmountRatio", getRatio(subscribeAmount, roomTotalAmount));
		}
		
		//查询签约总数与总金额
		SignedDto signedEntity = new SignedDto();
		signedEntity.setRemark(sql);
		SignedInfo signedInfo = signedService.totalNumberAndAmount(signedEntity);	
		if(signedInfo != null){
			//数量
			resMap.put("signedTotal", String.valueOf(signedInfo.getTotal()));
			resMap.put("signedTotalRatio", getRatio(new BigDecimal(signedInfo.getTotal()), 
					new BigDecimal(queryList.size())));
			//金额
			BigDecimal signAmount = new BigDecimal("0");
			if(signedInfo.getSignAmount() != null){
				signAmount = signedInfo.getSignAmount();
			}
			resMap.put("signedAmount", numberFormat(signAmount, new BigDecimal(10000), 6));
			resMap.put("signedAmountRatio", getRatio(signAmount, roomTotalAmount));
		}
		
		return resMap;
	}
	/**
	 * 查询房间总价
	 * @param id
	 * @return
	 */
	private BigDecimal getTotalPrice(String sql){
		BigDecimal totalPrice = new BigDecimal("0");
		PriceSchemeDetail priceScheme = priceSchemeDetailService.selectEntityByRoomIds(sql);
		if(priceScheme != null){
			totalPrice = priceScheme.getTotalPrice() == null ? 
					new BigDecimal("0") : priceScheme.getTotalPrice();
		}
		return totalPrice;
	}
	/**
	 * 计算比例
	 * @param divisor 除数
	 * @param dividend 被除数
	 * @return 
	 */
	private String getRatio(BigDecimal divisor, BigDecimal dividend){
	    //当除数为0时，返回结果为0
	    if(dividend.compareTo(new BigDecimal("0")) == 0){
	        return "0";
	    }
		//计算比例
		BigDecimal ratio = divisor.divide(dividend, 4, BigDecimal.ROUND_HALF_UP)
					.multiply(new BigDecimal("100"));
		//格式化比例
		DecimalFormat decimalFormat = new DecimalFormat("#.#");
		return decimalFormat.format(ratio);
	}
	
	/**
	 * 计算比例
	 * @param divisor 除数
	 * @param dividend 被除数
	 * @return 
	 */
	private String numberFormat(BigDecimal divisor, BigDecimal dividend, int point){
	    //当除数为0时，返回结果为0
	    if(dividend.compareTo(new BigDecimal("0")) == 0){
	        return "0";
	    }
		//计算比例
		BigDecimal ratio = divisor.divide(dividend, point, BigDecimal.ROUND_UNNECESSARY);
		//格式化比例
		DecimalFormat decimalFormat = new DecimalFormat("#.####");
		return decimalFormat.format(ratio);
	}
	
	
}
